package com.jp.knowledge.util;

import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.text.TextPaint;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jp.knowledge.R;

import java.lang.reflect.Field;

/**
 * Created by jiayen on 17/5/31.
 */

public class TabLayoutUtil {

    public static void tabIndicatorWithText(@NonNull TabLayout tabLayout){
        Field tabLayoutViewField = null;
        Field tabTextSizeField = null;
        try{
            tabLayoutViewField = TabLayout.class.getDeclaredField("mTabStrip");
            tabTextSizeField = TabLayout.class.getDeclaredField("mTabTextSize");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        tabLayoutViewField.setAccessible(true);
        tabTextSizeField.setAccessible(true);

        LinearLayout tabLayoutView = null;
        float tabTextSize = 0;
        try {
            tabLayoutView = (LinearLayout) tabLayoutViewField.get(tabLayout);
            tabTextSize = (float) tabTextSizeField.get(tabLayout);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        TextPaint paint = new TextPaint();
        paint.setTextSize(tabTextSize);
        for(int i = 0, j = tabLayout.getTabCount(); i < j; i++){
            View child = tabLayoutView.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            int tabWidth = child.getWidth();
            CharSequence text = tabLayout.getTabAt(i).getText();
            float textWidth = paint.measureText(text==null ? "" : text.toString());
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) child.getLayoutParams();
            params.leftMargin = (int) ((tabWidth-textWidth)/2);
            params.rightMargin = params.leftMargin;
        }

        tabLayoutViewField.setAccessible(false);
        tabTextSizeField.setAccessible(false);
    }

    public static void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());
        int size = llTab.getChildCount();
        for (int i = 0; i < size; i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }
}